package com.framework.loippi.service.impl;

import com.framework.loippi.dao.UserDao;
import com.framework.loippi.entity.Principal;
import com.framework.loippi.entity.User;
import com.framework.loippi.service.UserService;
import com.framework.loippi.utils.Paramap;
import org.apache.commons.collections.CollectionUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Service - 管理员
 * 
 * @author Loippi Team
 * @version 1.0
 */
@Service("userServiceImpl")
public class UserServiceImpl extends GenericServiceImpl<User, Long> implements UserService {
	
	@Autowired
	private UserDao userDao;
	
	@Autowired
	public void setGenericDao() {
		super.setGenericDao(userDao);
	}

	@Transactional(readOnly = true)
	public User getCurrent() {
		Subject subject = SecurityUtils.getSubject();
		if (subject != null) {
			Principal principal = (Principal) subject.getPrincipal();
			if (principal != null && principal.getId() != null) {
				return userDao.find(principal.getId());
			}
		}
		return null;
	}
	
	@Transactional(readOnly = true)
	public String getCurrentUsername() {
		Subject subject = SecurityUtils.getSubject();
		if (subject != null) {
			Principal principal = (Principal) subject.getPrincipal();
			if (principal != null) {
				return principal.getUsername();
			}
		}
		return null;
	}

	public boolean usernameExists(String usernames) {
		List<User> users = userDao.findByParams(Paramap.create().put("username", usernames));
		return CollectionUtils.isNotEmpty(users);
	}

	@Override
	@Transactional
	public Long openUser(Long[] ids) {
		User user = null;
		for (Long id : ids) {
			user = new User();
			user.setId(id);
			user.setIsEnabled(Boolean.TRUE);
			userDao.update(user);
		}
		return 1L;
	}

	@Override
	@Transactional
	public Long closeUser(Long[] ids) {
		User user = null;
		for (Long id : ids) {
			user = new User();
			user.setId(id);
			user.setIsEnabled(Boolean.FALSE);
			userDao.update(user);
		}
		return 1L;
	}

	@Override
	public List<User> findUserByRoleId(Long roleId) {
		return userDao.findUserByRoleId(roleId);
	}

	@Override
	public List<User> findByUserName(String username) {
		return userDao.findByUserName(username);
	}


}
